// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Ride Block Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 59 : TYPE_RIDEBLOCK

// Function declarations
reserve function RideBlock_DebugDraw
reserve function RideBlock_DebugSpawn

// Static Values

// Tables

function RideBlock_DebugDraw
	DrawSprite(0)
end function


function RideBlock_DebugSpawn
	CreateTempObject(TypeName[Ride Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].value2 = object[arrayPos0].xpos
	object[tempObjectPos].value3 = object[arrayPos0].ypos
end function


event ObjectMain
	switch object.state
	case 0
		object.priority = PRIORITY_ACTIVE
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -16, 16, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.state++
				if object.direction == FLIP_NONE
					object.xvel = 0x10000
				else
					object.xvel = -0x10000
				end if
			end if
		next
		break
	case 1
		temp0 = object.xpos
		temp0 -= object.value1
		temp0 += object.xvel
		temp0 &= -0x10000
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -16, 16, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].xpos += temp0
			end if
		next
		object.xpos += object.xvel
		if object.xvel > 0
			ObjectTileCollision(CSIDE_LWALL, 16, 12, 0)
			if checkResult == 1
				object.xvel = 0
				object.value0 = 0
				temp1 = object.iypos
				temp1 += 12
				Get16x16TileInfo(temp0, object.ixpos, temp1, TILEINFO_ANGLEB)
				if temp0 == 1
					object.state = 2
				else
					object.state = 3
				end if
			end if
		else
			ObjectTileCollision(CSIDE_RWALL, -16, 12, 0)
			if checkResult == 1
				object.xvel = 0
				object.value0 = 0
				temp1 = object.iypos
				temp1 += 12
				Get16x16TileInfo(temp0, object.ixpos, temp1, TILEINFO_ANGLEB)
				if temp0 == 1
					object.state = 2
				else
					object.state = 3
				end if
			end if
		end if
		break
	case 2
		if object.value0 < 320
			object.value0++
			temp0 = object.value0
			temp0 &= 7
			if temp0 == 0
				temp0 = 0x10000
			else
				temp0 = 0
			end if
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -16, 16, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object[currentPlayer].ypos += temp0
				end if
			next
			object.ypos += temp0
		end if
		break
	case 3
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -16, 16, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		break
	end switch
	object.value1 = object.xpos
	object.value1 &= -0x10000
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value2
		object.ypos = object.value3
		if object.outOfBounds == 1
			object.yvel = 0
			object.state = 0
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
end event


event ObjectDraw
	DrawSpriteXY(0, object.value1, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 159, 322)
	foreach (TypeName[Ride Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].direction = object[arrayPos0].propertyValue
		object[arrayPos0].direction &= FLIP_X
		object[arrayPos0].value2 = object[arrayPos0].xpos
		object[arrayPos0].value3 = object[arrayPos0].ypos
	next
	SetTableValue(TypeName[Ride Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(RideBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(RideBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
